package com.smarth.zhizulian.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.BaseFragment;
import com.smarth.zhizulian.app.LazyEventFragment;
import com.smarth.zhizulian.bean.Page;
import com.smarth.zhizulian.bean.message.MessageAppNews;
import com.smarth.zhizulian.bean.message.MessageAppNoticePage;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.GlideUtils;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.RMultiListDataAdapter;
import com.smarth.zhizulian.utils.listhelper.impl.model.HttpListDataModel;
import com.smarth.zhizulian.utils.listhelper.inter.ResponseSender;
import com.smarth.zhizulian.view.LoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by 18865 on 2018/2/28.
 */

public class NoticeFragment extends LazyEventFragment {
    @BindView(R.id.rc_recyclerview)
    RecyclerView rcRecyclerview;
    @BindView(R.id.rv_refresh)
    SmartRefreshLayout rvRefresh;
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    private RefreshHelper<List<MessageAppNoticePage>> refreshHelper;


    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setBKView(R.layout.global_fragment_rc);
//        initListView();
    }

    @Override
    protected void lazyLoad() {
        initListView();
    }

    private void initListView() {
        rcRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        refreshHelper = new RefreshHelper<>(mContext, llLoadinglayout, rvRefresh);
        refreshHelper.setDataModel(new HttpListDataModel<List<MessageAppNoticePage>>(mContext) {
            @Override
            public Observable<? extends Page> getObservable(ResponseSender<List<MessageAppNoticePage>> responseSender, int page) {
                return KumaHttp.getService().messageAppNoticePage(page).compose(RxCompos.<Page<MessageAppNoticePage>>getBELoginCompos());
            }
        });
        MyAdapter adapter = new MyAdapter(new ArrayList<MessageAppNoticePage>());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageAppNoticePage item = (MessageAppNoticePage) adapter.getData().get(position);
                if (!TextUtils.isEmpty(item.getNoticeLink())) {
                    AgentWebActivity.openForNotice(mContext, item.getNoticeLink());
                }
            }
        });
        refreshHelper.setDataAdapter(adapter);
        refreshHelper.refresh();
    }

    public class MyAdapter extends RMultiListDataAdapter<MessageAppNoticePage> {

        public MyAdapter(@Nullable List<MessageAppNoticePage> data) {
            super(data);
            addItemType(MessageAppNoticePage.TYPE_IMAGE, R.layout.item_notice_image);
            addItemType(MessageAppNoticePage.TYPE_NOIMAGE, R.layout.item_message_nomore);
        }

        @Override
        protected void convert(BaseViewHolder helper, MessageAppNoticePage item) {
            helper.setText(R.id.tv_time, item.getPublishTime());
            helper.setText(R.id.tv_title, item.getNoticeTitle());
            helper.setText(R.id.tv_content, item.getNoticeContent());
            switch (helper.getItemViewType()) {
                case MessageAppNoticePage.TYPE_IMAGE:
                    GlideUtils.loadNotice(mContext, item.getNoticeImage(), (ImageView) helper.getView(R.id.iv_image));
                    break;
                case MessageAppNoticePage.TYPE_NOIMAGE:
                    break;
            }
        }

        @Override
        public void onGetData(List<MessageAppNoticePage> messageAppNoticePages, boolean isRefresh) {
            super.onGetData(messageAppNoticePages, isRefresh);
            EventBus.getDefault().post(new EventBean("NoticeFragment"), BaseConfig.EventTag.ON_DEAL_NOTICE);
        }
    }

//    @Subscriber(tag = BaseConfig.EventTag.ON_CLEAR_MESSAGE)
//    private void onEventClearMessage(EventBean eb) {
//        refreshHelper.refresh();
//    }
}
